package com.springboot2.service.Impl;

import com.springboot2.entity.Menu;
import com.springboot2.entity.Org;
import com.springboot2.mapper.MenuMapper;
import com.springboot2.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public void add(Menu menu) {
        menuMapper.add(menu);
    }

    @Override
    public void del(int id) {
        menuMapper.del(id);
    }

    @Override
    public void update(Menu menu) {
        menuMapper.update(menu);
    }

    @Override
    public Menu findById(int id) {
        return menuMapper.findById(id);
    }

    @Override
    public List<Menu> findByList() {

        return menuMapper.findByList();
    }

    @Override
    public List<Menu> lists() {
        List<Menu> list = menuMapper.findByList();
        return childrenTree(null,list);
    }

    //递归生成 树状结构
    private List<Menu> childrenTree(Integer pid, List<Menu> allData){
        List<Menu> list = new ArrayList<>();
        for (Menu menu : allData) {
            if(Objects.equals(menu.getPid(),pid)){
                list.add(menu);
                List<Menu> childrenTree = childrenTree(menu.getId(),allData);
                menu.setChildren(childrenTree);
            }
        }
        return list;
    }

    @Override
    public List<Menu> findByPage(int pageNo, int pageSize) {
        Map<String,Integer> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        return menuMapper.findByPage(map);
    }

    @Override
    public int count() {
        return menuMapper.count();
    }

    @Override
    public List<Menu> findByPageParams(int pageNo, int pageSize, String name) {
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("name",name+"%");
        return menuMapper.findByPageParams(map);
    }

    @Override
    public int countParams() {
        return menuMapper.countParams();
    }

    @Override
    public List<Menu> findByName(String name) {
        Map<String,String> map = new HashMap<>();
        map.put("name",name+"%");
        return menuMapper.findByName(map);
    }


}
